package com.sx.service.basic;

import com.sx.basic.exception.BasicException;
import com.sx.basic.service.BasicService;
import com.sx.basic.service.impl.BasicServiceImpl;
import com.sx.dao.basic.FileRepository;
import com.sx.entity.basic.FileEntity;
import com.sx.service.utils.FileUploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author AdinZ
 * @date 2020/11/18 10:11
 */
@Service
public class FileService extends BasicServiceImpl<FileRepository, FileEntity,Long>
        implements BasicService<FileEntity,Long>,IFileService {

    @Resource
    private FileUploadService fileUploadService;


    @Override
    public String upload(MultipartFile file) throws IOException {
        FileEntity upload = fileUploadService.upload(file);
        if(upload==null|| StringUtils.isEmpty(upload.getPath()))throw new BasicException("文件上传失败");
        this.save(upload);
        return upload.getPath();
    }
}
